﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Restaurant.DAO
{

    public class NguyenLieuDB
    {
        private SqlConnection conn = KetNoiDB.TaoKetNoi();

        public DataTable LayNguyenLieu()
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spLayNguyenLieu";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return dbtab;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable LayNguyenLieuTheoMa(int ma)
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spLayNguyenLieuTheoMa";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maNL", ma));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return dbtab;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable LayNguyenLieuTheoLoai(int ma)
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spLayNguyenLieuTheoLoai";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maLoaiNL", ma));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return dbtab;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable LayNguyenLieuTheoDVT(int ma)
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spLayNguyenLieuTheoDVT";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maDVTinh", ma));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return dbtab;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable LayNguyenLieuTheoNCC(int ma)
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spLayNguyenLieuTheoNCC";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maNCC", ma));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return dbtab;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool ThemNguyenLieu(string tenNL, int maNCC, int maDVTinh, int maLoaiNL)
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spThemNguyenLieu";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@tenNL", tenNL));
                cmd.Parameters.Add(new SqlParameter("@maNCC", maNCC));
                cmd.Parameters.Add(new SqlParameter("@maDVTinh", maDVTinh));
                cmd.Parameters.Add(new SqlParameter("@maLoaiNL", maLoaiNL));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool XoaNguyenLieu(int maNL)
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spXoaNguyenLieu";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maNL", maNL));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool SuaNguyenLieu(int maNL, string tenNL, int maNCC, int maDVT, int maLoaiNL)
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spSuaNguyenLieu";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maLoaiNL", maLoaiNL));
                cmd.Parameters.Add(new SqlParameter("@tenNL", tenNL));
                cmd.Parameters.Add(new SqlParameter("@maNL", maNL));
                cmd.Parameters.Add(new SqlParameter("@maNCC", maNCC));
                cmd.Parameters.Add(new SqlParameter("@maDVTinh", maDVT));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
